package com.feihong.yw189.popupwindow;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.popupwindow.listener.BackSearchTypeListener;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Set;

public class SearchTypePopupWindow {

    private Activity mActivity;
    private TagFlowLayout mFlowLayout;
    private TextView tv_finish;
    //    private String[] mVals = new String[]{};
    private ArrayList<String> mVals = new ArrayList<>();
    private BackSearchTypeListener backListener;
    private String clickStr = "";

    /**
     * @param activity
     * @param backListener
     */
    public SearchTypePopupWindow(Activity activity, BackSearchTypeListener backListener) {
        this.mActivity = activity;
        this.backListener = backListener;
    }

    public void setData(ArrayList<String> data) {
        this.mVals = data;
        setMyAdapter();
        mFlowLayout.getAdapter().notifyDataChanged();
    }

    public void setMyAdapter() {

        mFlowLayout.setAdapter(new TagAdapter<String>(mVals) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(mActivity).inflate(R.layout.type_popu_tv,
                        mFlowLayout, false);


                tv.setText(s);
                return tv;
            }
        });
    }

    public PopupWindow showPopupWindow() {
        View convertView = LayoutInflater.from(mActivity).inflate(R.layout.data_popupwindow, null);
        final PopupWindow popupWindow = new PopupWindow(convertView);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(false);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mFlowLayout = (TagFlowLayout) convertView.findViewById(R.id.flowlayout);
        tv_finish = (TextView) convertView.findViewById(R.id.tv_finish);
        tv_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backListener.backPopType(clickStr);
            }
        });
        mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                Log.e("setOnSelectListener", "------clickable111------" + selectPosSet.toString());
            }
        });

        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                clickStr = mVals.get(position);
                return true;
            }

        });
        return popupWindow;
    }
}
